﻿// ***********************************************************************
// Assembly         : OSS.Doc
// Author           : tianteng
// Created          : 04-28-2020
//
// Last Modified By : tianteng
// Last Modified On : 04-29-2020
// ***********************************************************************
// <copyright file="NewSphereCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright ©  2020
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Linq;
using Occ.BRepPrimAPI;
using Occ.gp;
using Osg;
using ZFGK.Addins;
using ZFGK.DocFrame.Utility;
using ZFGK.OSG.DocUI;
using ZFGK.OSG.DocUI.CmdEnables;
using ZFGK.OSG.UI;
using ZFGK.OSG.UI.Actions;
using ZFGK.OSG.Utility;
using ZFGK.OsgOcc.Doc.CustomEntity;
using ZFGK.OsgOcc.Utility;
using Shape = Occ.TopoDS.Shape;
using Sphere = Osg.Sphere;

namespace OSS.Doc.Cmds
{
    /// <summary>
    /// Class NewSphereCmd.
    /// Implements the <see cref="ZFGK.Addins.IExtendCommand" />
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    [OnlyDocCmdEnable]
    public class NewSphereCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var docViewForm = cmdData.ViewForm as IViewForm;
            if (docViewForm == null) return CmdResult.Cancel;

            var osgViewCtrl = docViewForm.View as ZfOsgDocViewCtrl;
            var actionUtility = osgViewCtrl.ActionUtility;
            var activeDocument = osgViewCtrl.ActiveDocument;

            Vec3d worldPt1, worldPt2;
            if (!actionUtility.PickPoint("选择圆心", Matrixd.identity(), out worldPt1))
                return CmdResult.Cancel;

            // 临时绘制
            var osgTempDraw = osgViewCtrl.OsgObj.CreateTempDraw();
            if (!actionUtility.PickPoint("选择另一个点", Matrixd.identity(), out worldPt2, delegate (PickPointAction action)
            {
                action.MouseMoveAccept += delegate (Vec3d[] vec3ds)
                {
                    var sphere = new Sphere(worldPt1.ToVec3f(), (float)(worldPt1 - vec3ds.First()).length);
                    var geode = sphere.CreateDrawable(0.01f, new Vec4f(1, 1, 1, 1)).CreateGeode();
                    geode.Name = "1";
                    osgTempDraw.Draw(geode);
                };
            }))
                return CmdResult.Cancel;


            osgTempDraw.Dispose();

            // 使用OCC造型
            var radius = (worldPt1 - worldPt2).length;
            var makeBox = new MakeSphere(new Ax2(worldPt1.ToOccPnt(), new Dir(1, 0, 0)), radius);
            var ent = new ZfOsgOccEntity();
            ent.Shape = new Shape(makeBox.Solid);

            activeDocument.PostToModelSpace(ent, activeDocument.ActiveLayer);

            return CmdResult.Succeed;
        }
    }
}
